﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace TestForm1
{
    public partial class code : Form
    {
        public code()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ClassName.MethodName();

            string input_9001 = Yb.getYbBackObjTest("9001");

            dynamic sss = new ExpandoObject();
            sss.name = "yutrao";
            sss.description = "111";

            Console.WriteLine(sss.name);

            sss.SayHello = new Action(() => {
                Console.WriteLine("SayHello");
                }
            );

            sss.SayHello1 = new Action<Dictionary<string,object>>((dict) => {
                Console.WriteLine("SayHello1"+ dict["name"]);
                }
            );

            sss.SayHello();
            Dictionary<string,object> data1111 = new Dictionary<string,object>();
            data1111.Add("name", "11111");
            sss.SayHello1(data1111);

            // 直接调用接口
            Dictionary<string,object> data = new Dictionary<string,object>();
            data.Add("mdtrt_cert_type",string.Empty);
            data.Add("mdtrt_cert_no", string.Empty);
            data.Add("card_sn", string.Empty);
            data.Add("begntime", string.Empty);
            data.Add("psn_cert_type", string.Empty);
            data.Add("certno", string.Empty);
            data.Add("psn_name", string.Empty);
            dynamic dy = Yb.getYbBackObj("1101", data);
            MessageBox.Show(Convert.ToString(JsonConvert.SerializeObject(dy.output)));
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string jybh = textBox1.Text;
                textBox2.Text = string.Empty;
                textBox3.Text = string.Empty;

                GetDataFromSqlLite.SourceCode sc = GetDataFromSqlLite.GetSourceCode(jybh);
                if (sc == null || sc.jysr == null)
                {
                    MessageBox.Show("未查询到此交易编号");
                    return;
                }
                     
                string input = DynamicLoader.getMethod(sc.jysr, null, null).ToString();

                textBox2.Text = JsonUtils.formatJson(input);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string jybh = textBox1.Text;
            string jysr = textBox2.Text;

            if ("".Equals(jybh) || "".Equals(jysr))
            {
                MessageBox.Show("调用内容为空");
                return;
            }

            if (jybh == "yb")
            {
                string output_json = DynamicLoader.getMethod(GetDataFromSqlLite.GetSourceCode("yb").jysr, new Type[] { typeof(string) }, new object[] { jysr }).ToString();

                if ("".Equals(output_json))
                {
                    MessageBox.Show("返回值为空");
                    return;
                }
                try
                {
                    dynamic dd = DynamicLoader.getOutput(GetDataFromSqlLite.GetSourceCode(jybh).jysc, output_json);
                    if (dd.infcode == 0)
                    {
                        textBox3.Text = JsonUtils.formatJson(JsonConvert.SerializeObject(dd.output));
                    }
                    else
                    {
                        textBox3.Text = JsonUtils.formatJson(output_json);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("返回消息:" + ex.Message + "@" + output_json);
                }
            }
            else
            {
             var input_dict = new Dictionary<string, object>
            {
               { "infno", jybh },{ "input", jysr },{ "sign_no", ""},
               { "opter", "" },{ "opter_name", "" },
               { "mdtrtarea_admvs", ""},{ "insuplc_admdvs", ""},
            };

                string input_json = DynamicLoader.getMethod(GetDataFromSqlLite.GetSourceCode("json").jysr, new Type[] { typeof(Dictionary<string, object>) }, new object[] { input_dict }).ToString();
                var input_json_dict = new Dictionary<string, object> { { "infno", jybh }, { "jysr", input_json }, };
                string output_json = DynamicLoader.getMethod(GetDataFromSqlLite.GetSourceCode("yb").jysr, new Type[] { typeof(Dictionary<string, object>) }, new object[] { input_json_dict }).ToString();

                if ("".Equals(output_json))
                {
                    MessageBox.Show("返回值为空");
                    return;
                }

                try
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    serializer.MaxJsonLength = int.MaxValue;
                    Dictionary<string, object> rootObject = serializer.Deserialize<Dictionary<string, object>>(output_json);

                    int infcode = Convert.ToInt32(rootObject["infcode"]);
                    string err_msg = Convert.ToString(rootObject["err_msg"]);
                    var outputObject = rootObject["output"] as Dictionary<string, object>;
                    dynamic dd = DynamicLoader.getOutput(GetDataFromSqlLite.GetSourceCode(jybh).jysc, Convert.ToString(serializer.Serialize(outputObject)));
                    if (infcode == 0)
                    {
                        textBox3.Text = JsonUtils.formatJson(JsonConvert.SerializeObject(dd));
                    }
                    else
                    {
                        textBox3.Text = JsonUtils.formatJson(output_json);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("返回消息:" + ex.Message + "@" + output_json);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new CodeModify(textBox1.Text).Show();
        }
    }
}
